package com.qf.bookmanage.controller;

import cn.hutool.core.lang.hash.Hash;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.bookmanage.entity.Book;
import com.qf.bookmanage.service.IBookService;
import com.qf.bookmanage.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dongjl
 * @since 2023-11-23
 */
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private IBookService bookService;

    @PostMapping("/up")
    public String upload(MultipartFile file01) throws IOException {
        System.out.println(file01);
        file01.transferTo(new File("D:\\00qd-2304\\upload\\aaa.jpg"));
        return "ok";
    }


    @GetMapping("/get/{id}")
    public Book getBook(@PathVariable("id") Integer id){
        System.out.println(id);
        return bookService.getById(id);
    }

    @GetMapping("/getall")
    public HashMap<String,Object> getAll(Integer pagenum,Integer pagesize){
        System.out.println("pagenum="+pagenum+",pagesize="+pagesize);
        // 构造方法中的参数1：当前要查询第几页的数据，第二个参数：每页多少条数据
        Page<Book> bookPage = new Page<>(pagenum,pagesize);
        bookService.page(bookPage);
        // 注意，分页查询方法执行完成后， 所有的数据都被放到bookPage对象中了
        // 获取当前页的数据
        List<Book> records = bookPage.getRecords();
        // 获取总条数
        long total = bookPage.getTotal();
        // 获取总页数
        long pages = bookPage.getPages();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("code",1);
        hashMap.put("msg","ok");
//        hashMap.put("records",records);
//        hashMap.put("total",total);
//        hashMap.put("pages",pages);
        PageVo pageVo = new PageVo();
        pageVo.setPages(pages);
        pageVo.setTotal(total);
        pageVo.setRecords(records);
        hashMap.put("data",pageVo);
        return hashMap;
    }


    @DeleteMapping("/delete")
    public HashMap<String,Object> deleteBook(Integer[] ids){
        System.out.println("ids="+ids);
        // Integer[] ids 把数组转换为 集合
        boolean b = bookService.removeByIds(Arrays.asList(ids));
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("code","1");
        hashMap.put("msg","ok");
        hashMap.put("data",b);
        return hashMap;
    }


    @PutMapping("/update")
    public HashMap<String,Object> updateBook(@RequestBody  Book book){
        System.out.println("book="+book);
        boolean b = bookService.updateById(book);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("code","1");
        hashMap.put("msg","ok");
        hashMap.put("data",b);
        return hashMap;
    }

    @PostMapping("/add")
    public Book insertBook(@RequestBody Book book ){
        System.out.println("book="+book);
        Book book1 = bookService.addBook(book);
        System.out.println(book1);
        return book1;
    }

    @GetMapping("/getbook")
    public Book getBookById(Integer bookid){
        System.out.println("bookid="+bookid);
        Book book = bookService.getById(bookid);
        return book;
    }



}
